package com.example.administrator.jingpingou.fragment;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.administrator.jingpingou.R;
import com.example.administrator.jingpingou.activity.WebTaoActivity;
import com.example.administrator.jingpingou.adapter.NextThemeAdapter;
import com.example.administrator.jingpingou.bean.NextThemeBean;
import com.example.administrator.jingpingou.config.NetConfig;
import com.google.gson.Gson;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lenovo on 2016/8/30.
 */
public class NextThemeFragmentOne extends Fragment {
    private SwipeRefreshLayout mRefreshLayout;
    private RecyclerView mRecyclerView;
    private NextThemeAdapter mAdapter;
    private List<NextThemeBean.DataBean> mData;
    private GridLayoutManager mLayoutManager;
    private String zid;
    private String num_iid;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_next_theme,null);
        Bundle bundle=getArguments();
        zid = bundle.getString("zid");
        initView(view);
        initData();
        setData();
        return view;
    }

    private void initView(View view) {
        mRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.sl_activity_theme);
        mRecyclerView = (RecyclerView) view.findViewById(R.id.rv_activity_theme);
    }

    private void initData() {
        mData =new ArrayList<>();
        mAdapter =new NextThemeAdapter(mData,getActivity());
        loadMore(1);
        mAdapter.setOnItemClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Integer position=(Integer)v.getTag();
                 num_iid = mData.get(position).getNum_iid();
                NextThemeBean.DataBean dataBean = mData.get(position);
                Intent intent = new Intent();
                intent.setClass(getActivity(),WebTaoActivity.class);
                intent.putExtra("num_iid", NextThemeFragmentOne.this.num_iid);
                intent.putExtra("dataBean",dataBean);
                startActivity(intent);
//                Integer position=(Integer)v.getTag();
            }
        });
    }

    private void setData() {
        mLayoutManager = new GridLayoutManager(getActivity(),2);
        mRecyclerView.setLayoutManager(mLayoutManager);
        mRecyclerView.setAdapter(mAdapter);

        mRefreshLayout.setColorSchemeColors(Color.BLACK);

        mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (newState==RecyclerView.SCROLL_STATE_IDLE){
                    int last = mLayoutManager.findLastVisibleItemPosition();
                    if (last==mData.size()-1){
//                        loadMore(currentPage);
                    }
//                    view.setVisibility(View.GONE);
//                    mTextTitle.setVisibility(View.GONE);
                }
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
            }
        });

    }

    private void loadMore(final int position){
        String path =String.format(NetConfig.PATH_THEME_NEXT,zid);
        x.http().get(new RequestParams(path), new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                if (result!=null){
                    Gson gson = new Gson();
                    NextThemeBean nextThemeBean = gson.fromJson(result, NextThemeBean.class);
                    for (int i = 0; i < mData.size(); i++) {
                        num_iid = nextThemeBean.getData().get(i).getNum_iid();
                    }
//                    currentPage++;
                    if (position==1){
                        mData.clear();
                    }
                    mData.addAll(nextThemeBean.getData());
                    mAdapter.notifyDataSetChanged();
                    mRefreshLayout.setRefreshing(false);
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Toast.makeText(getActivity(),"网络连接失败.........",Toast.LENGTH_SHORT).show();
                mRefreshLayout.setRefreshing(false);
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }
}
